package com.liao.tensquare.admin.controller;

import com.liao.tensquare.annotation.Log;
import com.liao.tensquare.enums.OperatorType;
import com.liao.tensquare.exception.ParamsException;
import com.liao.tensquare.admin.service.ExcelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Map;

@Deprecated
@RestController
@RequestMapping(value = "excel")
public class ExcelController {

    @Autowired
    private ExcelService excelService;

    @Log(value = "根据角色Id下载角色的Excel",isSave = true,operType = OperatorType.EXPORT)
    @RequestMapping(value = "/role/list", method = RequestMethod.POST)
    public void getExcelForRole(HttpServletResponse response, @RequestBody Map<String, ArrayList<Integer>> map) {
        ArrayList<Integer> roleIds = map.get("roleIds");
        if (roleIds == null || roleIds.size() == 0) {
            throw new ParamsException("角色Id不能为空");
        }
        excelService.downExcelByRole(response, roleIds);
    }

    @Log(value = "下载角色所有的Excel",isSave = true,operType = OperatorType.EXPORT)
    @RequestMapping(value = "/role/all", method = RequestMethod.POST)
    public void getExcelForRoleAll(HttpServletResponse response) {
        excelService.downExcelByRoleAll(response);
    }

    @Log(value = "保存角色的Excel",isSave = true,operType = OperatorType.IMPORT)
    @RequestMapping(value = "/role/save", method = RequestMethod.POST)
    public void saveExcelForRole(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        String filename = multipartFile.getOriginalFilename();
        System.out.println("filename=" + filename);
        InputStream inputStream = multipartFile.getInputStream();
        excelService.saveExcelByRole(inputStream);
    }
}
